import json
import requests
from loguru import logger
from sche_task import schedulerIns
from app.core.conf import ST
from sio import sio



def register_temp_humi_event():
    schedulerIns.add_job(get_temp_and_humi, 'interval', seconds=2, args=[], id='temp_humi')
    # # all1  
    # try:
    #     # schedulerIns.remove_job("temp_humi")
    #     schedulerIns.add_job(get_temp_and_humi, 'interval', seconds=2, args=[], id='temp_humi')
    # except Exception as e:
    #     logger.error('暂时没有temp_humi的任务，直接添加');
    # return



async def get_temp_and_humi():
    try:
        r = requests.get(ST.TEMP_HUMI_URL)
        # logger.warning('send temp humi');
        # logger.warning(r.text);
        await sio.emit("temp_humi", eval(r.text))
    except Exception as e:
        logger.debug('无法温湿度，请检查相关硬件设备');
